package com.example.hzq.zqb.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.LoginActivity;

import static com.example.hzq.zqb.activity.MainActivity.loginflag;

/**
 * Created by hzq on 2017/7/14.
 */

public class Fragment4 extends Fragment{
    private View view=null;
    private Context context;
    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private DisplayMetrics dm;
    private NewsFragment fragment1=null,fragment2=null,fragment3=null,fragment4=null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment4, container, false);
        context = getActivity();
        init();
        return view;
    }
    private void init() {
        dm = getResources().getDisplayMetrics();//获取屏幕密度
        viewpager=(ViewPager)view.findViewById(R.id.f4_viewpage);
        viewpager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        pagersliding=(PagerSlidingTabStrip)view.findViewById(R.id.f4_pagesliding);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();
    }
    private void setpagerstyle() {
        pagersliding.setShouldExpand(true); // 设置Tab是自动填充满屏幕的
        pagersliding.setDividerColor(Color.TRANSPARENT); // 设置Tab的分割线是透明的
        // 设置Tab底部线的高度
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // 设置Tab Indicator的高度
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));
        // 设置Tab标题文字的大小
        pagersliding.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#45c01a"));// 设置Tab Indicator的颜色
        // 设置选中Tab文字的颜色 (这是自定义的一个方法)
        pagersliding.setSelectedTextColor(Color.parseColor("#45c01a"));
        pagersliding.setTabBackground(0); // 取消点击Tab时的背景色
    }
    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = {"校园兼职","企业兼职","分期商城","攒攒钱"};//显示在二级导航上的标题文字

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    if (fragment1 == null) {
                        fragment1 = new NewsFragment(0);
                    }
                    return fragment1;
                case 1:
                    if (fragment2 == null) {
                        fragment2 = new NewsFragment(1);
                    }
                    return fragment2;
                case 2:
                    if (fragment3 == null) {
                        fragment3 = new NewsFragment(2);
                    }
                    return fragment3;
                case 3:
                    if (fragment4 == null) {
                        fragment4 = new NewsFragment(3);
                    }
                    return fragment4;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

    }
}
